package com.allwees.bs.c.module.order.context.compute.strategy.shippinginsurance;

import com.allwees.bs.c.module.order.context.bo.CouponComputeBo;
import com.allwees.core.common.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Deprecated
public class ShippingInsuranceCalculateStrategy implements ShippingInsuranceStrategy {

    @Value("${system.params.order.shipping.insurance:0.99}")
    private String insurance;

    @Override
    public CouponComputeBo strategy(CouponComputeBo bo) {
        if (MoneyUtil.isLtEqZero(bo.getAmtShipping())) {
            return bo;
        }

        String value = this.insurance;
        if (StringUtils.isBlank(value) || !NumberUtils.isCreatable(value)) {
            return bo;
        }

        BigDecimal fullFree = new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_UP);
        if (bo.getAmtProduct().compareTo(fullFree) < 0) {
            return bo;
        }
        bo.getItems().forEach(item -> item.setOriginalAmtShippingSku(BigDecimal.ZERO));
        return bo.computeAmt();
    }
}
